<?php
function maxSubArray($nums)
{
    $array = $nums;
    $pre = 0;
    $maxAns = $array[0];
    foreach ($array as $value) {
        $pre = max($pre + $value, $value);
        $maxAns = max($pre, $maxAns);
    }
    return $maxAns;
}

function maxSubArray1($nums)
{
    $sum = $nums[0];
    $max = $nums[0];
    echo "nums[0]:" . $nums[0] . "|" . "sum:" . $sum . "|" . "max:" . $max . "<br/>";
    for ($i = 1; $i < count($nums); $i++) {
        if ($sum < 0)
            $sum = 0;    //sum<0 则sum直接更新不增反减，将sum归零再更新
        $sum += $nums[$i];
        $max = ($max < $sum) ? $sum : $max;   //取序列和最大值
        echo "nums[$i]:" . $nums[$i] . "|" . "sum:" . $sum . "|" . "max:" . $max . "<br/>";
    }
    return $max;
}

//var_dump(maxSubArray1([-2, 1, -3, 4, -1, 2, 1, -
var_dump(maxSubArray([3,-7,4]));

DELIMITER $$

USE `demo`$$

DROP PROCEDURE IF EXISTS `p_make_data`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `p_make_data`(l INT)
BEGIN
	DECLARE i INT DEFAULT 0;
	
	WHILE i < l DO
	    insert into `test` (`a`, `b`, `c`) 
		values(f_rand_num(100000000000, 999999999999), f_rand_str(8),f_rand_num(100000000000, 999999999999));
	    SET i = i + 1;
	    
	    IF (i % 1000 = 0) THEN
		select @i;
	    END IF;
	    
	END WHILE;
    END$$

DELIMITER ;
